#!/usr/bin/env python
#
#       Logic.py
#       
#       Copyright 2010 Propellor and Ratchet Games http://pandrgames.co.uk/
#      
from Clients import Clients
clients = Clients()



#NOTE:
#Logic could be used to rememebr stats from each game.
#damage taken and received, per player per ship, being the easy ones now
#once we have a good idea of average damage in battles, we can give trophies for high scores.

#stats could also be manipulated into scores, too; winning with minimum effort, mostly.
#yey


class Logic:
    def __init__(self, gametype=0):
        self.gametype = gametype
        self.running = False
        self.restartingnow = False
        self.won = False
        
        #KINGOFTHEHILL
        #1Ki, 4P
        #5P
        #Win: kill 1Ki or 5P
        
        #CHESS
        #1Ki, 1Q, 2Kn, 2B, 2R, 8P
        #Win: kill 1Ki
        
        #SPLIT
        #4P, 1Kn, 1B, 1R each
        #Win: kill all
        
    def gameon(self, ison="lol"):
        #is the game currently running?
        if ison == "lol":
            return self.running
        elif ison == True or ison == False:
            self.running = ison
            
    def restarting(self, ison="lol"):
        #are we resetting the game to lobby?
        if ison == "lol":
            return self.restartingnow
        elif ison == True or ison == False:
            self.restartingnow = ison
        
        if self.restartingnow:
            self.won = False
        
        
    def shipded(self, toclient, playerlist, player, ship):
        print "=L=: ", player, "'s ship", ship, "is dead!", len(playerlist[player].ships)-1 , "ships remaining."
        if self.gametype == 0:
            pass
        elif self.gametype == "KINGOFTHEHILL":
            if playerlist[player].ships[ship].type == "King":
                #player in playerlist who isn't player, wins
                newlist = []
                for guy in playerlist:
                    if guy != player:
                        newlist.append(guy)
                self.awin(toclient, newlist)
                
            elif len(playerlist[player].ships)-1 == 0:
                newlist = []
                for guy in playerlist:
                    if guy != player:
                        newlist.append(guy)
                self.awin(toclient, newlist)
                
            

    def shipdamaged(self, toclient, playerlist, p1, ship1, p2, ship2, damage):
        #when ship1 damages ship2
        if self.gametype == 0:
            pass
        
    
    
    #trigger zones, on move, get from map.
    
    
    def awin(self, tcl, playerlist):
        #list of keys of players who won, all others don't.
        print "=L=: Players",
        for player in playerlist:
            print player,
        print "WIN!"
        
        for client in clients:
            client.restartplz = 0
            winner = 0
            for player in playerlist:
                print client.player.key(), player
                if client.player.key() == player:
                    client.send(tcl.Message("You win!"))
                    client.send(tcl.Win())
                    winner = 1
            if winner == 0:
                client.send(tcl.Message("You lose!"))
                client.send(tcl.Lose())
        self.won = True
            